package com.epfresh.adapter;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.DisplayImageOptions;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.assist.ImageScaleType;
import com.epfresh.bean.ListStore;

import multitype.ItemViewBinder;

/**
 * Author: Chen
 * Date: 2019/2/27
 * Desc:
 */
public class MineAttentionViewBinder extends ItemViewBinder<ListStore, MineAttentionViewBinder.ViewHolder> {
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = inflater.inflate(R.layout.item_merchant, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final ListStore item) {
        holder.rl_title.setVisibility(View.GONE);

        ImageLoader.getInstance().displayImage(item.getHeaderImage(), holder.img_logo, options);
        holder.img_status.setVisibility(View.VISIBLE);
        holder.img_status.setImageResource(R.mipmap.icon_attention);
        holder.txt_merchant_name.setText(item.getName());
        if (item.getOrderCount() != null) {
            if (Integer.parseInt(item.getOrderCount()) >= 9999) {
                holder.txt_order_count.setText("订单量9999+");
            } else {
                holder.txt_order_count.setText("订单量" + item.getOrderCount());
            }
        }else {
            holder.txt_order_count.setText("订单量0");
        }
        if (item.getOnShelvesProductCount() != null) {
            if (Integer.parseInt(item.getOnShelvesProductCount()) >= 9999) {
                holder.txt_goods_count.setText("商品9999+");
            } else {
                holder.txt_goods_count.setText("商品" + item.getOnShelvesProductCount());
            }
        }else {
            holder.txt_goods_count.setText("商品0");
        }
        holder.txt_rating.setText("好评" + item.getGoodCommentsRate());
        holder.txt_wholesale_market.setText(item.getStall());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(item);
                }
            }
        });

    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private View rl_title;
        private ImageView img_logo, img_status;
        private TextView txt_merchant_name;
        private TextView txt_order_count, txt_goods_count, txt_rating;
        private TextView txt_wholesale_market;


        ViewHolder(View itemView) {
            super(itemView);

            rl_title = itemView.findViewById(R.id.rl_title);
            img_logo = itemView.findViewById(R.id.img_logo);
            img_status = itemView.findViewById(R.id.img_status);
            txt_merchant_name = itemView.findViewById(R.id.txt_merchant_name);
            txt_order_count = itemView.findViewById(R.id.txt_order_count);
            txt_goods_count = itemView.findViewById(R.id.txt_goods_count);
            txt_rating = itemView.findViewById(R.id.txt_rating);
            txt_wholesale_market = itemView.findViewById(R.id.txt_wholesale_market);
        }
    }

    private DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.icon_fail_merchant) //设置图片在下载期间显示的图片
            .showImageForEmptyUri(R.mipmap.icon_fail_merchant)//设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.mipmap.icon_fail_merchant)
            .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
            .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
            .considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    public interface OnItemClickListener {
        void onItemClick(ListStore item);
    }


}
